/*
 * Copyright (C) 2014 gujicheng
 * 鏈粡浣滆�璁稿彲锛岀姝㈠皢璇ョ▼搴忕敤浜庡晢涓氱敤閫� * 
 * 璇ュ０娉㈤�淇＄▼搴忓湪鍓嶄竴涓紑婧愮増鏈紙SinVoice锛夌殑鍩虹涓婏紝鍋氫簡璁稿浼樺寲锛� * 浼樺寲濡備笅锛� * 1. 璇嗗埆鏁堢巼鏇撮珮锛屽嚑涔庤揪鍒�00%锛屽畬鍏ㄥ彲浠ヨ揪鍒板晢涓氱敤閫旀爣鍑嗭紝姣攃hirp锛屾敮浠樺疂锛岃寗瀛愬揩浼犵瓑杞欢鐨勮瘑鍒晥鐜囨洿楂樸�
 * 2. 鑳芥敮鎸佹洿澶氬鏉傚満鏅殑璇嗗埆锛屽湪鏈夊槇鏉傚ぇ澹扮殑鑳屾櫙闊充箰锛屽槇鏉傜殑浼氳瀹わ紝椋熷爞锛屽叕浜よ溅锛岄┈璺紝鏂藉伐鍦哄湴锛� *     灏忔苯杞︼紝KTV绛変竴浜涘鏉傜殑鐜涓嬶紝渚濈劧鑳戒繚鎸佸緢楂樼殑璇嗗埆鐜囥�
 * 3. 鑳芥敮鎸佹洿澶歵oken鐨勮瘑鍒紝閫氳繃缂栫爜鍙互浼犻�鎵�湁瀛楃銆� * 4. 閫氳繃瀹氬埗鍙互瀹炵幇鐩稿悓瀛楃鐨勮繛缁紶閫�姣斿鈥�34456鈥濄�
 * 5. 鏀寔鑷姩绾犻敊鍔熻兘锛屽湪鏈�涓互鍐呭瓧绗﹁В鐮佸嚭閿欑殑鎯呭喌涓嬪彲浠ヨ嚜鍔ㄧ籂姝ｃ�
 * 6. 绋嬪簭杩愯鏁堢巼闈炲父楂橈紝鍙互鐢ㄤ簬鏅鸿兘鎵嬫満锛屽姛鑳芥墜鏈猴紝宓屽叆寮忚澶囷紝PC锛屽钩鏉跨瓑宓屽叆寮忕郴缁熶笂銆� * 7. 澹版尝鐨勯鐜囧０闊冲拰闊抽噺鍙畾鍒躲�
 * 
 * 姝emo绋嬪簭灞炰簬璇曠敤鎬ц川绋嬪簭锛屼粎鍏峰閮ㄥ垎鍔熻兘锛屽叾闄愬埗濡備笅锛� * 1. 浠呮敮鎸侀儴鍒嗗瓧绗﹁瘑鍒�
 * 2. 璇嗗埆鑻ュ共娆″悗锛岀▼搴忎細鑷姩鍋滄璇嗗埆銆傝嫢鎯崇户缁娇鐢紝璇峰仠姝㈣绋嬪簭锛岀劧鍚庨噸鏂板惎鍔ㄧ▼搴忋�
 * 3. 涓嶆敮鎸佽繛缁瓧绗︿紶閫掋�
 * 4. 涓嶆敮鎸佽嚜鍔ㄧ籂閿欏姛鑳姐�
 * 5. 绂佹鐢ㄤ簬鍟嗕笟鐢ㄩ�銆� * 
 * 鑻ユ偍瀵瑰畬鏁寸殑澹版尝閫氫俊绋嬪簭鎰熷叴瓒ｏ紝璇疯仈绯讳綔鑰呰幏鍙栧晢涓氭巿鏉冪増鏈紙浠呮敹鍙栬嫤閫肩殑鍔犵彮璐癸級銆� *************************************************************************
 **                   浣滆�淇℃伅                                                            **
 *************************************************************************
 ** Email: gujicheng197@126.com                                        **
 ** QQ   : 29600731                                                                 **
 ** Weibo: http://weibo.com/gujicheng197                          **
 *************************************************************************
 */
package com.edgar.skill;


import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;

import android.media.AudioTrack;
import android.os.Bundle;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;


import android.view.View.OnTouchListener;
import android.widget.Button;


import android.widget.RelativeLayout;

import android.media.AudioFormat;


import android.media.AudioManager;




public class VoicePlayActivity extends Activity  {
    private final static String TAG = "VoicePlayActivity";
    
    RelativeLayout maiview;
    AudioTrack track;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }
    
    void initView(){ 
		maiview = new RelativeLayout(this);
		RelativeLayout.LayoutParams lp_main = new RelativeLayout.LayoutParams(
		          ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
		maiview.setLayoutParams(lp_main);
		this.setContentView(maiview);

		Button sendBtn = new Button(this); 
		sendBtn.setText("播放"); 
		sendBtn.setTextSize(16);
		sendBtn.setId(2031);
		sendBtn.setPadding(10, 10, 10, 10);
		maiview.addView(sendBtn);
		
		sendBtn.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				
				switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					play();
			            
					break;
				case MotionEvent.ACTION_UP:
					stop();
					
					break;
				}
				
				return false;
			}});

		
	 }
    @Override
    public void onResume() {
        super.onResume();

    }
    void play(){
    	int bufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
    
    	System.out.println("bufferSize "+bufferSize);
    	track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize, AudioTrack.MODE_STREAM);
   
    	track.play();
    	
    	InputStream is;
		try {
			is = this.getResources().getAssets().open("voice.mp3");
	    	byte[] audioData = new byte[1024];
	    	while((is.read(audioData)!=-1)){
	    		track.write(audioData, 0, 1024);
	    	}
	    	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


    }
    
    void stop(){
    	track.stop();
    	track.release();
    }
    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
